package com.ibm.crl.sb.thread.others.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 详细见 http://blog.csdn.net/waterbig/article/details/4794244
 * 
 * @author shangbin
 * 
 */
public class TestThreadPool {

	private static int produceTaskSleepTime = 2;

	private static int produceTaskMaxNumber = 10;

	public static void main(String[] args) {
		Executors.newCachedThreadPool();
		// 构造一个线程池
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3,
				TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
				new RejectedExecutionHandler() {

					@Override
					public void rejectedExecution(Runnable r,
							ThreadPoolExecutor executor) {
						System.out.println("舍弃"
								+ ((ThreadPoolTask) r).getTask());
					}
				});
		// new ThreadPoolExecutor.DiscardOldestPolicy());

		for (int i = 1; i <= produceTaskMaxNumber; i++) {
			try {
				String task = "task@ " + i;
				System.out.println("创建任务并提交到线程池中：" + task);
				threadPool.execute(new ThreadPoolTask(task));

				Thread.sleep(produceTaskSleepTime);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}